/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Mainterface.java
 *
 * Created on 18/01/2011, 21:28:17
 */

package xmldots;
import Points.*;
import FileReadWrite.*;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.Stack;
import javax.swing.BorderFactory;
import javax.swing.JColorChooser;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.filechooser.FileFilter;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;



/**
 *
 * @author Avishai Kopeliansky 
 */
public class Mainterface extends javax.swing.JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = -223007495779205384L;
	Color colo,colo2;
	Point T;
	PointContainer pc;
	Stack<Point> st;
	Read r;
	Write w;
	int _x , _y , maxby , maxbx;
	Graphics g;
	JFileChooser fc;
	JColorChooser col;
	FileFilter Filef;
	boolean painter,eraser;
	/** Creates new form Mainterface */
	public Mainterface() {
		initComponents();
		pc =  new PointContainer();
		g = ActionArea.getGraphics();
		maxbx =631;
		maxby =320;
		colo2= colo = Color.black;
		st = new Stack<Point>();
		Filef  = new ExtensionFilter("XML File" , ".xml");
		painter = false;
		eraser = false;
		Undo.setEnabled(false);
		Redo.setEnabled(false);      
		this.setLocationRelativeTo(null);

	}

	/**
	 *Gets cord's from the current mouse location and draws a dot on the panel;
	 * @param e MouseEvent - ActionEvent MouseClick
	 */
	public void drawDot(MouseEvent e){
		_x=e.getX();
		_y=e.getY();
		if( _x < 0 )
			_x = 0;
		else   if( _x > maxbx)
			_x = maxbx;
		if(_y < 0 )
			_y=0;    
		else if (_y > maxby)
			_y = maxby;

		T = new Point(_x,_y, colo);
		if(!eraser){
			g.setColor(colo);
			g.fillOval(_x, _y,5 ,5);
			pc.add(T);
			Undo.setEnabled(true);
		}
		else{
			g.setColor(Color.white);
			g.fillOval(_x, _y,7 ,7);
			pc.add(T);
		}

		Output.setText(" X = "+_x+ " Y = "+_y);


	}


	//Second saving function recieves the File name from the Overloaded Function
	private void Saving(String s){
		filling();
		try{
			w= new Write(pc);
			w.Writer(s,Output);
		}
		catch (Exception e){
			Output.setText(e.getMessage());
		}
	}
	//Secong loading function recieves the File name from the Overloaded Function
	private PointContainer Loading(String s){
		try {
			r = new Read();
			pc=r.Reader(s);
		}
		catch(Exception e){
			Output.setText(e.getMessage());
		}
		return pc;
	}


	private void undoer()
	{
		if(!pc.isEmpty())
		{
			st.push(pc.elementAt(pc.size()-1));
			g.setColor(Color.white);
			g.fillOval(pc.elementAt(pc.size()-1).GetX(),pc.elementAt(pc.size()-1).GetY(),5 ,5);
			pc.remove(pc.elementAt(pc.size()-1));
			if(pc.isEmpty()) Undo.setEnabled(false);
			Redo.setEnabled(true);
		}

	}
	private void redoer()
	{
		if(!st.empty())
		{
			@SuppressWarnings("unused")
			int x , y;
			x = st.peek().GetX();
			y = st.peek().GetY();
			pc.add(st.pop());
			filling();
			if(st.empty()) Redo.setEnabled(false);
			Undo.setEnabled(true);

		}
	}
	//Drawing the loaded Dots from the Point Container
	private void filling(){
		int x=0, y=0;

		for(int i=0 ; i < pc.size() ; i++){
			x = pc.elementAt(i).GetX();
			y = pc.elementAt(i).GetY();
			colo =new Color(pc.elementAt(i).getRed(),pc.elementAt(i).getGreen(),pc.elementAt(i).getBlue());
			g.setColor(colo);
			g.fillOval(x ,y,5 ,5);
		}
	}
	//Draws all white
	private boolean exstexist(String s)
	{
		return s.endsWith("xml");
	}
	private void clearer(){
		int x=0, y=0;

		for(int i=0 ; i < pc.size() ; i++){
			x = pc.elementAt(i).GetX();
			y = pc.elementAt(i).GetY();

			g.setColor(Color.white);
			g.fillOval(x ,y,5 ,5);
		}
		pc = new PointContainer();
		Undo.setEnabled(false);
		Redo.setEnabled(false);
		st.removeAllElements();

	}
	//Overloaded Saving function , creates a file and calls the Writing function with its name
	private void Saving(){
		filling();
		try{       
			fc = new JFileChooser(".");
			fc.setFileFilter(Filef);
			fc.setDialogTitle("Save As...");
			fc.showSaveDialog(Saver);       
			File f = fc.getSelectedFile();
			String dest = f.getPath();
			if(!(exstexist(dest))){
				dest+=".xml";
				Saving(dest);
			}
			else
				Saving(dest);
		}
		catch(Exception e){};
	}
	//Overloaded Loading function , creates a file and calls the Reading function with its name
	private void Loading(){
		filling();
		try{
			fc = new JFileChooser(".");
			fc.setFileFilter(Filef);
			fc.setDialogTitle("Select File");
			fc.showOpenDialog(Opener);
			File file = fc.getSelectedFile();
			String s = file.getPath();
			clearer();
			Loading(s);
			filling();
			Output.setText("File Loaded");
		}
		catch( Exception e){};


	}


	
	// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		ActionArea = new javax.swing.JPanel();
		Output = new javax.swing.JTextField();
		jCheckBox1 = new javax.swing.JCheckBox();
		Eraser = new javax.swing.JCheckBox();
		ColorCh = new javax.swing.JButton();
		jButton1 = new javax.swing.JButton();
		jMenuBar1 = new javax.swing.JMenuBar();
		jMenu1 = new javax.swing.JMenu();
		Saver = new javax.swing.JMenuItem();
		Opener = new javax.swing.JMenuItem();
		jMenuItem1 = new javax.swing.JMenuItem();
		jMenu2 = new javax.swing.JMenu();
		Clear = new javax.swing.JMenuItem();
		Undo = new javax.swing.JMenuItem();
		Redo = new javax.swing.JMenuItem();
		jMenu3 = new javax.swing.JMenu();
		jMenuItem2 = new javax.swing.JMenuItem();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		setTitle("XML Assignment by Avishai Kopeliansky");
		setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
		setResizable(false);

		ActionArea.setBackground(new java.awt.Color(255, 255, 255));
		ActionArea.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
		ActionArea.setDebugGraphicsOptions(javax.swing.DebugGraphics.BUFFERED_OPTION);
		ActionArea.setDoubleBuffered(false);
		ActionArea.setMaximumSize(new java.awt.Dimension(633, 320));
		ActionArea.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent evt) {
				ActionAreaMouseClicked(evt);
			}
		});
		ActionArea.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
			public void mouseDragged(java.awt.event.MouseEvent evt) {
				ActionAreaMouseDragged(evt);
			}
		});

		javax.swing.GroupLayout ActionAreaLayout = new javax.swing.GroupLayout(ActionArea);
		ActionArea.setLayout(ActionAreaLayout);
		ActionAreaLayout.setHorizontalGroup(
				ActionAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 636, Short.MAX_VALUE)
				);
		ActionAreaLayout.setVerticalGroup(
				ActionAreaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGap(0, 325, Short.MAX_VALUE)
				);

		Output.setEditable(false);
		Output.setText("X =  Y = ");

		jCheckBox1.setText("Paint");
		jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jCheckBox1ActionPerformed(evt);
			}
		});

		Eraser.setText("Eraser");
		Eraser.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				EraserActionPerformed(evt);
			}
		});

		ColorCh.setText("Color");
		ColorCh.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ColorChActionPerformed(evt);
			}
		});

		jButton1.setText("Bug Fix");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		jMenu1.setText("File");

		Saver.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
		Saver.setText("Save");
		Saver.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				SaverActionPerformed(evt);
			}
		});
		Saver.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyPressed(java.awt.event.KeyEvent evt) {
				SaverKeyPressed(evt);
			}
		});
		jMenu1.add(Saver);

		Opener.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
		Opener.setText("Open");
		Opener.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				OpenerActionPerformed(evt);
			}
		});
		Opener.addKeyListener(new java.awt.event.KeyAdapter() {
			public void keyPressed(java.awt.event.KeyEvent evt) {
				OpenerKeyPressed(evt);
			}
		});
		jMenu1.add(Opener);

		jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
		jMenuItem1.setText("Exit");
		jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jMenuItem1ActionPerformed(evt);
			}
		});
		jMenu1.add(jMenuItem1);

		jMenuBar1.add(jMenu1);

		jMenu2.setText("Edit");

		Clear.setText("Clear");
		Clear.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				ClearActionPerformed(evt);
			}
		});
		jMenu2.add(Clear);

		Undo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK));
		Undo.setText("Undo");
		Undo.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				UndoActionPerformed(evt);
			}
		});
		jMenu2.add(Undo);

		Redo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Y, java.awt.event.InputEvent.CTRL_MASK));
		Redo.setText("Redo");
		Redo.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				RedoActionPerformed(evt);
			}
		});
		jMenu2.add(Redo);

		jMenuBar1.add(jMenu2);

		jMenu3.setText("Help");

		jMenuItem2.setText("About");
		jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jMenuItem2ActionPerformed(evt);
			}
		});
		jMenu3.add(jMenuItem2);

		jMenuBar1.add(jMenu3);

		setJMenuBar(jMenuBar1);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
		layout.setHorizontalGroup(
			layout.createParallelGroup(Alignment.LEADING)
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addGroup(layout.createParallelGroup(Alignment.LEADING)
						.addGroup(layout.createSequentialGroup()
							.addComponent(jCheckBox1)
							.addGap(24)
							.addComponent(Eraser)
							.addGap(62)
							.addComponent(Output, GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
							.addGap(37)
							.addComponent(jButton1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addGap(18)
							.addComponent(ColorCh, GroupLayout.PREFERRED_SIZE, 73, GroupLayout.PREFERRED_SIZE)
							.addGap(23))
						.addComponent(ActionArea, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addContainerGap())
		);
		layout.setVerticalGroup(
			layout.createParallelGroup(Alignment.TRAILING)
				.addGroup(layout.createSequentialGroup()
					.addContainerGap()
					.addComponent(ActionArea, GroupLayout.DEFAULT_SIZE, 325, Short.MAX_VALUE)
					.addGap(18)
					.addGroup(layout.createParallelGroup(Alignment.BASELINE)
						.addGroup(layout.createSequentialGroup()
							.addGap(3)
							.addComponent(Output, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(Eraser)
						.addComponent(jCheckBox1)
						.addComponent(jButton1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(ColorCh, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
					.addContainerGap())
		);
		getContentPane().setLayout(layout);

		pack();
	}// </editor-fold>//GEN-END:initComponents

	private void ActionAreaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ActionAreaMouseClicked
		if(!st.empty()){
			st.removeAllElements();
			Redo.setEnabled(false);          
		}
		drawDot(evt);

	}//GEN-LAST:event_ActionAreaMouseClicked

	private void ActionAreaMouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ActionAreaMouseDragged
		if(painter)   drawDot(evt);
	}//GEN-LAST:event_ActionAreaMouseDragged

	private void SaverKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_SaverKeyPressed
		Saving();
	}//GEN-LAST:event_SaverKeyPressed

	private void SaverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaverActionPerformed
		Saving();
	}//GEN-LAST:event_SaverActionPerformed

	private void ClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ClearActionPerformed
		clearer();
	}//GEN-LAST:event_ClearActionPerformed

	private void OpenerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OpenerActionPerformed
		Loading();
	}//GEN-LAST:event_OpenerActionPerformed

	private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
		this.dispose();
	}//GEN-LAST:event_jMenuItem1ActionPerformed

	private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
		new AboutScreen().setVisible(true);
		

	}//GEN-LAST:event_jMenuItem2ActionPerformed

	private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
		if(painter)
			painter = false;
		else
			painter = true;
	}//GEN-LAST:event_jCheckBox1ActionPerformed

	private void OpenerKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_OpenerKeyPressed
		Loading();
	}//GEN-LAST:event_OpenerKeyPressed

	private void EraserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_EraserActionPerformed
		if(eraser)
		{
			eraser = false;
			colo = colo2;
			ActionArea.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
		}
		else
		{
			colo2=colo;
			eraser = true;
			colo = Color.white;
			ActionArea.setCursor(new java.awt.Cursor(java.awt.Cursor.CROSSHAIR_CURSOR));

		}
	}//GEN-LAST:event_EraserActionPerformed

	private void UndoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UndoActionPerformed
		undoer();
	}//GEN-LAST:event_UndoActionPerformed

	private void RedoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RedoActionPerformed
		redoer();
	}//GEN-LAST:event_RedoActionPerformed

	private void ColorChActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ColorChActionPerformed
		choosColor();
	}//GEN-LAST:event_ColorChActionPerformed

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
		// TODO add your handling code here:
		filling();
	}//GEN-LAST:event_jButton1ActionPerformed

	@SuppressWarnings("static-access")
	private void choosColor(){
		col = new JColorChooser();
		colo= col.showDialog(new JPanel(), "ColorChoose", colo);
		col.setBorder(BorderFactory.createTitledBorder(
				"Choose Text Color"));
	}
	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new Mainterface().setVisible(true);

			}
		});
	}

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JPanel ActionArea;
	private javax.swing.JMenuItem Clear;
	private javax.swing.JButton ColorCh;
	private javax.swing.JCheckBox Eraser;
	private javax.swing.JMenuItem Opener;
	private javax.swing.JTextField Output;
	private javax.swing.JMenuItem Redo;
	private javax.swing.JMenuItem Saver;
	private javax.swing.JMenuItem Undo;
	private javax.swing.JButton jButton1;
	private javax.swing.JCheckBox jCheckBox1;
	private javax.swing.JMenu jMenu1;
	private javax.swing.JMenu jMenu2;
	private javax.swing.JMenu jMenu3;
	private javax.swing.JMenuBar jMenuBar1;
	private javax.swing.JMenuItem jMenuItem1;
	private javax.swing.JMenuItem jMenuItem2;
	// End of variables declaration//GEN-END:variables

}
